package com.hu.green.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hu.green.commen.R;
import com.hu.green.entity.Comment;
import com.hu.green.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin(value = "*")
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    /**
     * 发布评论
     */
    @PostMapping ("/post")
    public R post(@RequestBody Comment comment) {
        // 评论实体传来帖子id、发布者账号
        commentService.addComment(comment);
        return R.success("发布成功");
    }

    /**
     * 删除评论
     * @param comment
     * @return
     */
    @PostMapping ("/delete")
    public R delete(@RequestBody Comment comment) {
        return null;
    }

    /**
     * 获取评论
     */
    @GetMapping ("/{id}")
    public List<Map<String,Map<String,Object>>> get(@PathVariable Integer id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getId,id);
        List<Comment> list = commentService.list(queryWrapper);

        List<Map<String,Map<String,Object>>> listMap = new ArrayList<Map<String,Map<String,Object>>>();

        for (Comment comment:list){
            Map<String,Map<String,Object>> mapMax = new HashMap<String,Map<String,Object>>();

            Map<String,Object> map1 = new HashMap<String,Object>();
            Map<String,Object> map2 = new HashMap<String,Object>();


            map1.put("img",comment.getUrl());
            map1.put("name",comment.getName());

            map2.put("comment",comment.getMessage());

            mapMax.put("userInfo",map1);
            mapMax.put("comment",map2);

            listMap.add(mapMax);
        }
        return listMap;
    }
}
